package com.itheima.semaphore.demo;

import java.util.concurrent.Semaphore;

public class MyRunnable implements Runnable {
    //创建管理员对象
    private Semaphore semaphore = new Semaphore(5);
    @Override
    public void run() {
        try{
            //获取通行证
            semaphore.acquire();
            //开始行驶
            System.out.println(Thread.currentThread().getName() + "号车获得了通行证开始行驶");
            Thread.sleep(500);
            System.out.println(Thread.currentThread().getName() + "号车归还了通行证");
            //归还通行证
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
